package where2go.sctu.com.zhiyou.adpater;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.db.AppraiseViewHolder;
import where2go.sctu.com.zhiyou.entity.Appraise;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.adpater
 * Created by 张某人
 * CreatedTime:on 2018/5/27   23:50
 * Description:
 */

public class AppraiseAdapter extends RecyclerView.Adapter<AppraiseViewHolder> {

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private String url = "http://47.104.80.155/youyiyou/resources/img/";

    private Context context;
    private List<Appraise.DataBean> dataBeanList=new ArrayList<Appraise.DataBean>();
    private Appraise.DataBean.CommentUserBean dataBean=new Appraise.DataBean.CommentUserBean();

    public AppraiseAdapter(Context context, List<Appraise.DataBean> dataBeanList) {
        this.context = context;
        this.dataBeanList = dataBeanList;
    }

    @Override
    public AppraiseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View  view= LayoutInflater.from(context).inflate(R.layout.item_appraise,
                parent,false);
        AppraiseViewHolder holder=new AppraiseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final AppraiseViewHolder holder, int position) {
            Appraise.DataBean data=dataBeanList.get(position);
            holder.appraiseGradeTv.setText("满意");
            holder.appraiseContentTv.setText(data.getCommentContent());
            String s=format.format(new Date(data.getCommentDate()));
            holder.appraiseTimeTv.setText(s);
            holder.nickNameTv.setText(data.getCommentUser().getUserName());
        ImageRequest imageRequest = new ImageRequest(url + dataBeanList.get(position).getCommentUser().getUserAvatarAddress(),
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                        holder.circleImageView.setImageBitmap(bitmap);
                    }
                }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //请求失败 设置默认图片

                holder.circleImageView.setImageResource(R.drawable.avatar);
            }

        });
        MyApplication.getHttpQueues().add(imageRequest);

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return dataBeanList != null ? dataBeanList.size() : 0;
    }
}
